package com.cictec.middleware.tde.rm.disp.receiver.handler;

import java.util.Date;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.cictec.middleware.tde.rm.disp.annotation.HandleMessages;
import com.cictec.middleware.tde.rm.disp.receiver.AbstractReceiverHandler;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x07TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x84PlatformMessage;

/**
 * 0x07 请求时间校正
 * 触发方式：自动触发或手动
 * 中心反馈：接收指令应答确认，中心下发校对时间数据包（0x84）
 * @author Administrator
 *
 */
@Component
@HandleMessages(0X07)
public class TerminalTimingRequestHandler extends AbstractReceiverHandler {

	private static Logger logger = LoggerFactory.getLogger(TerminalTimingRequestHandler.class);

	@Override
	public void handleMessage(TerminalMessage message, IoSession session) {

		logger.debug("【0x07 请求时间校正】终端 {} Session {}  ", message.getTerminalId(), message.getSessionId());

		sendMessageToTerminal(createResponse(message, 1), session);

		Rm0x07TerminalMessage msg = (Rm0x07TerminalMessage) message;
		
		Header header = new Header(0x84);
		header.setSendTime(new Date());
		header.setLineId(msg.getHeader().getLineId());
		header.setTerminalId(msg.getHeader().getTerminalId());
		header.setDataFlag(0);

		Rm0x84PlatformMessage pr = new Rm0x84PlatformMessage(header);
		pr.setSessionId(msg.getSessionId());

		sendMessageToTerminal(pr, session);
		

		
	}

}
